<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/8/12
 * Time: 9:51
 */
class GetSwitchInfo extends ApiPublicBase
{
    public function logic()
    {
        $platform = $this->platform;
        $channelId = $this->channelId;
        $ver = $this->ver;
        $result = [];

        //所有平台，所有渠道，所有版本的开关设置
        $result = $this->getSwitchValue($result, 0, 0, 0);
        //当前平台，所有渠道，所有版本的开关设置
        $result = $this->getSwitchValue($result, $platform, 0, 0);
        //当前平台，当前渠道，所有版本的开关设置
        $result = $this->getSwitchValue($result, $platform, $channelId, 0);
        //当前平台，当前渠道，当前版本的开关设置
        $result = $this->getSwitchValue($result, $platform, $channelId, $ver);

        if (empty($result)) {
            $this->returnError('no_switch_info');
        }

        $this->returnData('suc_action', $result);
    }

    /**
     * 获取开关设置
     * @param $result
     * @param $platform
     * @param $channelId
     * @param $ver
     * @return array
     */
    public function getSwitchValue($result, $platform, $channelId, $ver)
    {
        $key = 'SWH:' . $platform . $channelId . $ver;

        if ($this->redis->exists($key)) {
            $data = $this->redis->get($key);
            $data = json_decode($data, true);
            $result = array_merge($result, $data);
        }

        return $result;
    }
}